<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="90px">
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          clearable
          placeholder="请输入企业名称"
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域" prop="region">
        <el-input
          v-model="queryParams.region"
          clearable
          placeholder="请输入企业名称"
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="短信状态" prop="sendStatus">
        <el-select v-model="queryParams.sendStatus" placeholder="请选择">
          <el-option label="全部" value="" />
          <el-option label="发送成功" value="1" />
          <el-option label="发送失败" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">
          查询
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">
          重置
        </el-button>
      </el-form-item>
    </el-form>
  
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          plain
          size="mini"
          type="danger"
          @click="handleSend"
        >
          一键发送短信
        </el-button>
        <el-button
          plain
          size="mini"
          type="danger"
          :disabled="multiple"
          @click="handleBatchSend(1)"
        >
          批量发送短信
        </el-button>
        <!-- <el-button
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >
          批量删除短信
        </el-button>
         -->
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList()" />
    </el-row>
  
    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="企业名称" prop="enterpriseName" />
      <el-table-column align="center" label="区域" prop="region" />
      <el-table-column :show-overflow-tooltip="true" align="center" label="联系人" prop="contacts" />
      <el-table-column :show-overflow-tooltip="true" align="center" label="联系方式" prop="phone" />
      <el-table-column :show-overflow-tooltip="true" align="center" label="短信内容" prop="content" />
      <el-table-column :show-overflow-tooltip="true" align="center" label="发送状态" prop="sendStatus" />
      <el-table-column :show-overflow-tooltip="true" align="center" label="发送时间" prop="sendTime" />
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.reSend"
            size="mini"
            type="text"
            @click="handleReSend(scope.row)"
          >
            重新发送
          </el-button>
          <el-button
            v-if="scope.row.sendStatus == '待发送'"
            size="mini"
            type="text"
            @click="handleBatchSend(2,scope.row)"
          >
            发送
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  
    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
  </div>
</template>
  
  <script>
  import {
    deleteLibrary,
    getMsgList,
    getSendMsgInfoList,
    reSendMsg,
    oneKeySendMsg,
    batchSendFormUrlMsg
  } from '../../../api/project/publish'
  export default {
    name: 'SampleLibrary',
    components:{
    },
    data() {
      
      return {
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 参数表格数据
        configList: [],
    
        // 类型数据字典
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          enterpriseName:undefined,
          sendStatus: undefined,
          region: undefined
        },
      }
    },
    created() {
      this.formKey = this.$route.query.key
      this.getList()
    },
    methods: {
      /** 查询参数列表 */
      getList() {
        this.loading = true
        var params={
          formKey:this.formKey,
          ...this.queryParams
        }
        console.log(params,'params')
        getSendMsgInfoList(params).then((response) => {
            this.configList = response.data.records
            this.total = response.data.total
            this.loading = false
          }
        )
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.sampleId)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      
      handleSend(row){
        var params={formKey:this.formKey}
        this.$confirm('是否一键发送短信给关联样本库?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
           return oneKeySendMsg(params)
        }).then(() => {
          this.getList()
          this.msgSuccess('发送成功')
        }).catch(() => {
        })
      },
      // 重新发送
      handleReSend(row){
        var msgId = row.msgId
        this.$confirm('是否重新发送短信?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return reSendMsg(msgId)
        }).then(() => {
          this.msgSuccess('重新发送成功')
        }).catch(() => {
        })
      },
      handleBatchSend(flag,row){
        var ids=[]
        if(row){
          ids = [row.sampleId] || this.ids
        }else{
          ids = this.ids
        }
        
        var params={
          formKey:this.formKey,
          sampleIds:ids
        }
        this.$confirm('是否'+(flag == 1 ? '批量' : '')+'发送短信?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return batchSendFormUrlMsg(params)
        }).then(() => {
          this.msgSuccess(flag == 1 ? '批量发送成功' :'发送成功')
          this.getList()
        }).catch(() => {
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids
        this.$confirm('是否确认删除?', '警告', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(function () {
          return deleteLibrary(ids)
        }).then(() => {
          var pageSize =Math.ceil((this.total-(row.id ? 1 : this.ids.length ))/this.queryParams.pageSize)
          if(pageSize < this.queryParams.pageNum){
              this.queryParams.pageNum = pageSize
          }
          this.getList()
          this.msgSuccess('删除成功')
        }).catch(() => {
        })
      },
    }
  }
  </script>
  